﻿using ICSharpCode.TextEditor.Document;
using QD.SQLHelper;
using QD.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QD.DbManager
{
    public partial class SQLContainer : DbBasic
    {
        private string txt;
        EFDbContext dbContext;
        LogonConfig logonConfig;
        private bool isEnableDb;

        public SQLContainer(LogonConfig logonConfig, string curDbName, string txt, bool isEnableDb)
            : this(logonConfig, curDbName, txt)
        {
            this.isEnableDb = isEnableDb;
        }

        public SQLContainer(LogonConfig logonConfig, string curDbName, string txt)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.txt = txt;

            this.logonConfig = logonConfig;
            string dbConnStr = logonConfig.GetConnectionStrings("master");
            EFDbContext masterContext = new EFDbContext(dbConnStr);
            IList<SysDatabases> databases = masterContext.Databases();
            foreach (SysDatabases db in databases)
            {
                this.comDbSource.Items.Add(db.name);
            }

            dbContext = new EFDbContext(logonConfig.GetConnectionStrings(curDbName));

            this.comDbSource.Text = curDbName;
        }

        public SQLContainer(string txt)
        {
            InitializeComponent();
            this.txt = txt;
            this.button2.Enabled = false;
            this.button3.Enabled = false;
        }

        private void TextContainer_Load(object sender, EventArgs e)
        {
            this.dataGridView1.InitGrid();
            this.dataGridView1.AutoGenerateColumns = true;

            this.textEditorControl1.ShowEOLMarkers = false;
            this.textEditorControl1.ShowHRuler = false;
            this.textEditorControl1.ShowInvalidLines = false;
            this.textEditorControl1.ShowMatchingBracket = true;
            this.textEditorControl1.ShowSpaces = false;
            this.textEditorControl1.ShowTabs = false;
            this.textEditorControl1.ShowVRuler = false;
            this.textEditorControl1.AllowCaretBeyondEOL = false;
            this.textEditorControl1.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("TSQL");
            this.textEditorControl1.Encoding = Encoding.GetEncoding("GB2312");

            this.textEditorControl1.Text = txt;
            this.comDbSource.Enabled = isEnableDb;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.label1.Visible = true;
            string str = this.textEditorControl1.Text.Trim();
            if (!string.IsNullOrEmpty(str))
            {
                Clipboard.Clear();
                Clipboard.SetText(str);
                this.label1.Text = "复制成功！";
            }
            else
            {
                this.label1.Text = "内容为空，复制失败！";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Tools.PromptHelper.PromptOkCancel("确定执行sql命令？") == DialogResult.OK)
            {
                try
                {
                    string sql = this.textEditorControl1.Text.Trim();
                    if (string.IsNullOrEmpty(sql))
                    {
                        Tools.PromptHelper.Prompt("请输入sql命令！");
                    }
                    else if ((sql.ToLower().Contains("update ")
                        || sql.ToLower().Contains("insert ")
                        || sql.ToLower().Contains("delete ")
                        ) && sql.Contains(".."))
                    {
                        Tools.PromptHelper.Prompt("不能跨数据库执行增、删、改命令！");
                    }
                    else
                    {
                        DataTable dt = dbContext.Query(sql);
                        this.dataGridView1.DataSource = dt;
                    }
                }
                catch (Exception ex)
                {
                    Tools.PromptHelper.Prompt(ex);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = this.textEditorControl1.Text.Trim();
            if (string.IsNullOrEmpty(sql))
            {
                Tools.PromptHelper.Prompt("请输入sql命令！");
            }
            else
            {
                string mes = string.Empty;
                bool isCorrect = dbContext.IsCorrectSQL(sql, ref mes);
                if (!isCorrect)
                {
                    Tools.PromptHelper.Prompt("sql命令验证不成功，信息如下：\r\n" + mes);
                }
                else
                {
                    Tools.PromptHelper.Prompt("sql命令验证成功！");
                }
            }
        }

        private void comDbSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            dbContext = new EFDbContext(logonConfig.GetConnectionStrings(this.comDbSource.Text));
        }
    }
}
